package com.example.server.db.tables

import com.example.commons.core.longValue
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.LongIdTable
import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.javatime.datetime
import java.time.LocalDateTime

object Permissions : LongIdTable() {
    val permName = varchar("permName", 64)

    /**
     * users:list
     * users:update
     * users:add
     */
    val permKey = varchar("permKey", 128)
    val enabled = bool("isEnabled").default(true)
    val createdAt = datetime("createdAt").clientDefault {
        LocalDateTime.now()
    }

//    val id = long("id").autoIncrement()
//    override val primaryKey = PrimaryKey(id)
}
//
//class Permission(id: EntityID<Long>) : LongEntity(id) {
//    companion object : LongEntityClass<Permission>(Permissions)
//
//    var permName by Permissions.permName
//    var permKey by Permissions.permKey
//    var enabled by Permissions.enabled
//    var createdAt by Permissions.createdAt
////    var city by City referencedOn Users.city
////    var age by Users.age
//}

//
//object Users2 : LongIdTable("users") {
//    //    val id = long("id").autoIncrement()
//    val username = varchar("username", 128)
//    val nickname = varchar("nickname", 128)
//    val password = varchar("password", 128)
//    val createdAt = long("createdAt").clientDefault {
//        LocalDateTime.now().longValue
//    }.nullable()
//
////    override val primaryKey = PrimaryKey(id)
//}


object UserPermission : Table() {
    val userId = long("userId").references(Users.id)
    val permId = long("permId").references(Permissions.id)
    val createdAt = datetime("createdAt").clientDefault {
        LocalDateTime.now()
    }

    override val primaryKey: PrimaryKey
        get() = PrimaryKey(userId, permId)
}


//fun testQuery() = Permissions.